<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="分类名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getNewsCategory">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="newsCategory" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="300">
            </el-table-column>
            <el-table-column prop="category" label="新闻分类" width="600" :show-overflow-tooltip="true" sortable>
            </el-table-column>
            <el-table-column label="操作" width="160">
                <!--<template scope="scope">  scope 属性在2.5以后的版本中已经废弃， 被 slot-scope 替代-->
                <template slot-scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="新闻分类" prop="title">
                    <el-input v-model="editForm.category" auto-complete="off"></el-input>
                </el-form-item>
                <!--<el-form-item label="新闻分类">-->
                <!--<el-select v-model="editForm.category" placeholder="请选择">-->
                <!--<el-option-->
                <!--v-for="item in options"-->
                <!--:key="item.id"-->
                <!--:label="item.category"-->
                <!--:value="item.category">-->
                <!--</el-option>-->
                <!--</el-select>-->
                <!--</el-form-item>-->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="新闻分类" prop="category">
                    <el-input v-model="addForm.category" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    export default {

        data() {
            return {
                filters: {
                    name: ''
                },
                newsCategory: [],
                listLoading: false,
                total: 0,
                page: 0,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '请输入分类', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: 0,
                    category: '',

                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        {required: true, message: '请输入分类', trigger: 'blur'}
                    ]
                },
                //新增界面数据
                addForm: {
                    category: '',
                },
                // options:[],
            }
        },
        methods: {
            handleCurrentChange(val) {
                this.page = val - 1;
                this.getNewsCategory();
            },
            //获取新闻列表
            getNewsCategory() {
                this.listLoading = true;
                let para = {category: this.filters.name}
                this.$ajax.get('newsCategory/getNewsCategory/' + this.page,{
                    params: {
                        category: this.filters.name
                    }
                })
                    .then((res) => {
                        this.total = res.data.message.totalElements;
                        this.newsCategory = res.data.message.content;
                        this.listLoading = false;
                    })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {id: row.id};
                    removeUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getNewsCategory();
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);

                // if(this.options.length === 0){
                //     this.$ajax.get('newsCategory/getNewsCategory')
                //         .then(res=>{
                //             this.options = res.data.message;
                //         }).catch(err=>{
                //         console.log(err);
                //     })
                // }
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    category: '',
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                            this.$ajax.post('newsCategory/addNewsCategory', para).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getNewsCategory();
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            this.$ajax.post('newsCategory/addNewsCategory', para).then((res) => {
                                this.addLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getNewsCategory();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {ids: ids};
                    batchRemoveUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getNewsCategory();
                    });
                }).catch(() => {

                });
            }
        },
        created() {
            this.getNewsCategory();
        }
    }

</script>

<style scoped>
    .el-input-number__decrease, .el-input-number__increase {
        position: absolute;
        z-index: 1;
        top: 1px;
        width: 40px;
        height: 38px;
        text-align: center;
        background: #f5f7fa;
        color: #606266;
        cursor: pointer;
        font-size: 13px;
    }

    [class*=" el-icon-"], [class^=el-icon-] {
        font-family: element-icons !important;
        speak: none;
        font-style: normal;
        font-weight: 400;
        font-variant: normal;
        text-transform: none;
        line-height: 3;
        vertical-align: baseline;
        display: inline-block;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    .el-dialog--small {
        width: 60%;
    }
</style>